<?php


namespace app\admin\model;


use think\Model;

class Base extends Model
{
    /*
     * 模型基础类
     * */

    protected $title='';
    protected $power='';
    /*自动日志*/
    public function createAndLog(array $data, array $allowField = [], bool $replace = false, string $suffix = '')
    {
        if($this->power){
            if(!power($this->power.'_create','创建'.$this->title,$this->power)){
                session('power','没有权限'.'(创建'.$this->title.')');
                return false;
            }
        }
        $msg = $this->model_log_msg($data);

        $model = $this->create($data, $allowField, $replace, $suffix);
        if($model){
            Log::create([
                'user'=>session('token')?decrypt(session('token')):'服务器',
                'value'=>'新建'.$this->title.'('.$model->id.':'.$msg.')',
                'info'=>is_array($this)?json_encode($this):json_encode($this->toArray())
            ]);
        }
        return $model;
    }

    public function deleteAndLog(): bool
    {
        if($this->power){
            if(!power($this->power.'_delete','删除'.$this->title,$this->power)){
                session('power','没有权限'.'(删除'.$this->title.')');
                return false;
            }
        }
        $msg = $this->model_this_log_msg($this);

        $delete = $this->delete();
        if($delete){
            Log::create([
                'user'=>session('token')?decrypt(session('token')):'服务器',
                'value'=>'删除'.$this->title.'('.$this->id.':'.$msg.')',
                'info'=>is_array($this)?json_encode($this):json_encode($this->toArray())
            ]);
        }
        return $delete;
    }
    public function saveAndLog(array $data = [], string $sequence = null): bool
    {
        if($this->power){
            if(!power($this->power.'_update','更新'.$this->title,$this->power)){
                session('power','没有权限'.'(更新'.$this->title.')');
                return false;
            }
        }
        $msg = $this->model_this_log_msg($this);

        Log::create([
            'user'=>session('token')?decrypt(session('token')):'服务器',
            'value'=>'更新'.$this->title.'('.$this->id.':'.$msg.')',
            'info'=>is_array($this)?json_encode($this):json_encode($this->toArray())
        ]);
        return $this->save($data,$sequence);
    }
    /*自动日志 end*/


    protected function model_log_msg($data): string
    {
        $msg = '';
        if(isset($data['title'])){
            $msg = $data['title'];
        }elseif (isset($data['name'])){
            $msg = $data['name'];
        }elseif (isset($data['username'])){
            $msg = $data['username'];
        }elseif (isset($data['out_trade_no'])){
            $msg = $data['out_trade_no'];
        }
        return $msg;
    }

    protected function model_this_log_msg($data): string
    {
        $msg = '';
        if(isset($data->title)){
            $msg = $data->title;
        }elseif (isset($data->name)){
            $msg = $data->name;
        }elseif (isset($data->username)){
            $msg = $data->username;
        }elseif (isset($data->out_trade_no)){
            $msg = $data->out_trade_no;
        }
        return $msg;
    }
}